(* Release 3.00 *)

(*    c_io1 - common low level I/O *)

(*    Copyright (C) 1989 Jensen and Partners. All Rights Reserved*)
(*    Written by Simon Knight.*)

(*    Copyright (C) 1991 Psion PLC. All Rights Reserved*)
(*    Modified by NSM.*)

include "corelib.inc"
include "epocdefs.inc"

module cio1

(***************************************************************************)
segment _TEXT(CODE,28H)
segment _DATA(DATA,28H)

extrn __filbuf

select _TEXT
(*
int fgetc(FILE *st)
*)

public _fgetc :

    push bx
    push cx
    push dx
    push si
    mov  bx, ax
    mov  cx, [bx][_flag]
    dec  word [bx][_cnt]
    js   callfunc
    mov  si, [bx][_ptr]
    inc  word [bx][_ptr]
    mov  al, [si]
    sub  ah, ah
done:
    test cx, _F_BIN
    jnz  noTrans
    cmp  al, CTLZ
    je   fgEOF
    cmp  al, 0DH
    jne  noTrans
    mov  ax, bx
    call _fgetc
noTrans:
    pop  si
    pop  dx
    pop  cx
    pop  bx
    ret  0

callfunc:
    push bx
    mov  ax, bx
    call __filbuf
    pop  bx
    jmp  done

fgEOF:
    or  word [bx][_flag], _F_EOF
    mov  ax, -1
    jmp  noTrans

(* _fgetc ENDP *)

(******************************************************************************)
section
segment _TEXT(CODE,28H)
segment _DATA(DATA,28H)

extrn __flsbuf
extrn __flusher

select _TEXT
(*
int  fputc(int ch, FILE *st)
*)

public _fputc :

    push cx
    push dx
    push si
    test word [bx][_flag], _F_BIN
    jnz  fpOK
    cmp  al, -1
    je   fpError
    cmp  al, CTLZ
    je   fpEOF
    cmp  al, 0AH
    jne  fpOK
    push bx
    mov  ax, 0DH
    call _fputc
    pop  bx
    mov  ax, 0AH
fpOK:
    dec  word [bx][_cnt]
    js   callfunc
    mov  si, [bx][_ptr]
    inc  word [bx][_ptr]
    mov  [si], al
    cmp  al, 0AH
    jne  done
    test word [bx][_flag], _F_LBUF
    jz   done
    mov  ax, bx
    call __flusher
done:
    pop  si
    pop  dx
    pop  cx
    ret  0

callfunc:
    call __flsbuf
    jmp  done
fpError:
    mov  ax, -1
    jmp  done

fpEOF:
    or  word [bx][_flag], _F_CTZ
    jmp  fpOK

(* _fputc ENDP *)

(****************************************************************************)
section
segment _TEXT(CODE,28H)
segment _DATA(DATA, 28H)

select _DATA

extrn __ungotchar

select _TEXT
public  _ungetch :
(*
int  ungetch(int c)
Returns character to console input stream. Returns EOF if __ungotchar
is occupied.
*)

    cmp  word [__ungotchar], -1
    jne  occupied
    cmp  ax, -1
    je   occupied
    mov  [__ungotchar], ax
    jmp  ungetchDone
occupied:
    mov  ax, EOF
ungetchDone:
    ret  0

(* _ungetch ENDP *)

(***************************************************************************)
section
segment _TEXT(CODE,28H)

extrn __ioerr

public  _chmod :
(*
int  chmod(char *path, int attribute)
Returns -1 on error and sets _errno.
*)

    push bx
    push cx
    push di
    mov  cx, bx
    mov  bx, ax
    mov	 di, 1
    cmp  cx, S_IREAD
    je   chRdonly
    mov	 cx, FileAttWrite
    jmp  chGotmode
chRdonly:
    sub	 cx, cx
chGotmode:
    sub	 al, al
    mov	 ah, NmFilStatusSet
    int	 FilManager
    jnc  chSuccess
    call __ioerr                            (* set errno *)
    mov  ax, -1
    jmp  chDone
chSuccess:
    sub  ax, ax
chDone:
    pop  di
    pop  cx
    pop  bx
    ret  0

(******************************************************************************)
section
segment _TEXT(CODE,28H)

extrn __ioerr

public  __chmod :
(*
int  _chmod(char *path, int mode, int attribute)
mode 0 - returns file attribute.
mode 1 - sets file attribute to attribute.
Returns -1 on error. Low level DOS function.
*)

    push bx
    push cx
    push di
    sub	 sp, FileEnt
    test bx, bx
    mov  bx, ax
    jz   getAtt
    mov  di, FileAttWrite | FileAttHidden | FileAttSystem
    and  cx, di
    xor	 cl, FileAttWrite
    sub	 al, al
    mov	 ah, NmFilStatusSet
    int	 FilManager
    jnc  chSuccess
chFailed:
    call __ioerr                            (* set errno *)
    mov  ax, -1
    jmp  chDone
getAtt:
    mov	 cx, sp
    sub	 al, al
    mov	 ah, NmFilStatusGet
    int  FilManager
    jc	 chFailed
    mov	 bx, sp
    mov	 ax, [bx][FileAtt]
    and  ax, FileAttWrite | FileAttHidden | FileAttSystem
    xor	 al, FileAttWrite
chSuccess:
    sub  ax, ax
chDone:
    add  sp, FileEnt
    pop  di
    pop  cx
    pop  bx
    ret  0

(******************************************************************************)
section
segment _TEXT(CODE,28H)

extrn __ioerr

public  _access :
(*
int  access(char *path, int mode)
Returns 0 if mode available.
Returns -1 on error.
*)

exist = 0
write = 2
read  = 4
rdwr  = 6

    push bx
    push cx
    push dx
    sub	 sp, FileEnt
    mov  cx, sp
    mov  dx, bx
    mov  bx, ax
    sub  al, al
    mov	 ah, NmFilStatusGet
    int  FilManager
    jnc  accSuccess
    call __ioerr                            (* set errno *)
accNo:
    mov  ax, -1
    jmp  accDone
accSuccess:
    test dx, dx
    je	 accYes
    test dx, write
    jz   accYes
    mov	 bx, sp
    test byte [bx][FileAtt], FileAttWrite
    jz	 accNo
accYes:
    sub  ax, ax
accDone:
    add	 sp, FileEnt
    pop  dx
    pop  cx
    pop	 bx
    ret  0

end


